package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by michaelbottjer on 18/04/2014.
 */
public class Customer {

//    Note that a customer's library number is composed of two things, first three digits are his customer number and the last four digits are the last four numbers of his telephone number

    ArrayList<Book> booksInPossession;
    ArrayList<Movie> moviesInPosession;
    HashMap<String, String> details;
    public static int customerNumber;
    private String password = new String();

    public Customer(String customerName, String customerEmail, String phoneNumber){
        booksInPossession = new ArrayList<Book>();
        moviesInPosession = new ArrayList<Movie>();
        details = new HashMap<String, String>();
        details.put("name", customerName);
        details.put("email", customerEmail);
        details.put("number", phoneNumber);
        customerNumber++;
        password = "mySecret";
    }

    public <T extends Item> void takeItemFromTo(int number, ArrayList<T> availableItems, ArrayList<T> itemsInPosession) {
        itemsInPosession.add(getRequestedItem(number, availableItems));
    }

    public <T extends Item> T getRequestedItem(int number, ArrayList<T> availableItems){
        return availableItems.get(number - 1);
    }

    public <T extends Item> void removeItem(int number, ArrayList<T> collectionOfBooks) {
        collectionOfBooks.remove(number-1);
    }

    public <T extends Item> void addAndRemoveBook(int number, ArrayList<T> moveFromThisList, ArrayList<T> moveToThisList){
        takeItemFromTo(number, moveFromThisList, moveToThisList);
        removeItem(number, moveFromThisList);
    }

    public <T extends Item> void checkOutItemFromTo(int number, ArrayList<T> moveFromThisList, ArrayList<T> moveToThisList, Library library) throws IndexOutOfBoundsException {
        try {
            library.currentlyCheckedOutItems.put(moveFromThisList.get(number-1).getTitle(), this.details.get("name"));
            addAndRemoveBook(number, moveFromThisList, moveToThisList);
            System.out.println("Thank you! Enjoy the item");
        }catch(IndexOutOfBoundsException ex) {
            System.out.println("That item is not available.");
        }
    }

    public <T extends Item> void returnItem(int number, ArrayList<T> moveFromThisList, ArrayList<T> moveToThisList, Library library) throws IndexOutOfBoundsException {
        try {
            library.removeFromCheckedOutList(number, moveFromThisList);
            addAndRemoveBook(number, moveFromThisList, moveToThisList);
            System.out.println("Thank you for returning the item.");
        }catch(IndexOutOfBoundsException ex){
            System.out.println("That item is not available.");
        }

    }

    public <T extends Item> void currentlyHolding(ArrayList<T> itemsInPosession) {
        for(int i = 0; i < itemsInPosession.size(); i++){
            System.out.println((i+1) + "." + " " + itemsInPosession.get(i).getTitle());
        }
    }

    public String threeNumberDigit(int customerNumber) {
        String firstThreeDigits;
        if(customerNumber>100){
            firstThreeDigits = Integer.toString(customerNumber);
        }else if(customerNumber>9){
            firstThreeDigits = "0" + Integer.toString(customerNumber);
        }else{
            firstThreeDigits = "00" + Integer.toString(customerNumber);
        }
        return firstThreeDigits;
    }

    public String createLastFourLibraryNumbers(String number) {
        String numbers = "" ;
        Matcher numberMatcher = Pattern.compile("[0-9]{4}$").matcher(number) ;
        if( numberMatcher.find() ) {
            numbers = numberMatcher.group(0) ;
        }
        return numbers;
    }

    public String libraryNumber() {
        return threeNumberDigit(customerNumber) + "-" + createLastFourLibraryNumbers(details.get("number"));
    }

    public String password() {
        return password;
    }
}
